package com.niji.littly_donkey_android.activity;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.NetworkGlideImageLoader;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

public class OrderDetailsActivity extends AppCompatActivity {

    private static final String TAG = "OrderDetailsActivity";

    private Button order_details_return;

    private String orderId;

    private Button order_details_copy;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_details_activity);
        ini();
        orderDetailsReturn();
        copyNumbering();
    }

    /**
     * 复制订单编号
     */
    private void copyNumbering() {
        order_details_copy = findViewById(R.id.order_details_copy);
        TextView order_details_numbering = findViewById(R.id.order_details_numbering);
        order_details_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(order_details_numbering.getText());
                Toast.makeText(OrderDetailsActivity.this, "复制成功",Toast.LENGTH_SHORT).show();//自定义的toast

            }
        });
    }

    /**
     * 初始化
     */
    private void ini() {
        Intent intent = getIntent();
        orderId = intent.getStringExtra("order_id");
        getOrderDetail(Integer.parseInt(orderId));
    }

    /**
     * 获取订单详情
     * @param
     */
    private void getOrderDetail(int orderId) {
        TextView order_details_status = findViewById(R.id.order_details_status);
        RelativeLayout order_details_status_info = findViewById(R.id.order_details_status_info);
        TextView order_details_ticket_name = findViewById(R.id.order_details_ticket_name);
        TextView order_details_ticket_price = findViewById(R.id.order_details_ticket_price);
        TextView order_details_ticket_number = findViewById(R.id.order_details_ticket_number);
        TextView order_details_subscribe_time = findViewById(R.id.order_details_subscribe_time);
        TextView order_details_uu_code = findViewById(R.id.order_details_uu_code);
        TextView order_details_name = findViewById(R.id.order_details_name);
        TextView order_details_phone = findViewById(R.id.order_details_phone);
        TextView order_details_ticket_priceSum = findViewById(R.id.order_details_ticket_priceSum);
        TextView order_details_numbering = findViewById(R.id.order_details_numbering);
        TextView order_details_order_time = findViewById(R.id.order_details_order_time);
        ImageView order_details_qr_code = findViewById(R.id.order_details_qr_code);
        Button order_details_btn1 = findViewById(R.id.order_details_btn1);
        Button order_details_btn2 = findViewById(R.id.order_details_btn2);
        String url = UrlUtil.URL + "/order/detail?order_id=" + orderId;
        RequestQueue requestQueue= Volley.newRequestQueue(OrderDetailsActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object>  data = (Map<String, Object>) map.get("data");
                        Log.i(TAG,data.toString());
                        String numbering = (String) data.get("numbering");
                        Integer tickets_number = (Integer) data.get("tickets_number");
                        String price = (String) data.get("price");
                        String playtime = (String) data.get("playtime");
                        String uu_code = (String) data.get("uu_code");
                        String qr_code_image = (String) data.get("qr_code_image");
                        Integer is_pay = (Integer) data.get("is_pay");
                        Integer status = (Integer) data.get("status");
                        String order_time = (String) data.get("order_time");
                        String subscribe_time = (String) data.get("subscribe_time");
                        Map<String,String> user_info = (Map<String, String>) data.get("user_info");
                        String name = user_info.get("name");
                        String phone = user_info.get("phone");
                        order_details_ticket_price.setText("￥" + price);
                        order_details_ticket_number.setText("×" + tickets_number + "张");
                        order_details_subscribe_time.setText("预定时间 " + subscribe_time);
                        order_details_uu_code.setText(uu_code);
                        order_details_name.setText("游客：" + name);
                        order_details_phone.setText("手机号：" + phone);
                        int sum = Integer.parseInt(price) * tickets_number;
                        order_details_ticket_priceSum.setText("￥" + sum);
                        order_details_numbering.setText(numbering);
                        order_details_order_time.setText(order_time);
                        if (qr_code_image != null){
                            if (!qr_code_image.equals("")){
                                NetworkGlideImageLoader.getInstance().displayImage(OrderDetailsActivity.this,qr_code_image,order_details_qr_code,0,0);
                            }
                        }

                        if(status == 1){
                            if(is_pay == 1){
                                order_details_status.setText("未支付");
                                order_details_status_info.setVisibility(View.VISIBLE);
                            }else if (is_pay == 2){
                                order_details_status.setText("已支付");
                                order_details_status_info.setVisibility(View.INVISIBLE);
                            }
                            order_details_btn1.setText("付款");
                            order_details_btn1.setBackground(getDrawable(R.drawable.my_order_red));
                            order_details_btn2.setText("取消订单");
                            order_details_btn2.setBackground(getDrawable(R.drawable.my_order_grey));
                        }else if(status == 2){
                            order_details_status.setText("待检票");
                            order_details_status_info.setVisibility(View.INVISIBLE);
                            order_details_btn1.setVisibility(View.INVISIBLE);
                            order_details_btn2.setText("退款");
                            order_details_btn2.setBackground(getDrawable(R.drawable.my_order_yellow));
                        }else if(status == 3){
                            order_details_status.setText("待评价");
                            order_details_status_info.setVisibility(View.INVISIBLE);
                            order_details_btn1.setText("再次购买");
                            order_details_btn1.setBackground(getDrawable(R.drawable.my_order_red));
                            order_details_btn2.setText("评价");
                            order_details_btn2.setBackground(getDrawable(R.drawable.my_order_yellow));
                        }else if(status == 4){
                            order_details_status.setText("退款");
                            order_details_status_info.setVisibility(View.INVISIBLE);
                            order_details_btn1.setText("再次购买");
                            order_details_btn1.setBackground(getDrawable(R.drawable.my_order_red));
                            order_details_btn2.setText("删除订单");
                            order_details_btn2.setBackground(getDrawable(R.drawable.my_order_grey));
                        }else if(status == 5){
                            order_details_status.setText("取消");
                            order_details_status_info.setVisibility(View.INVISIBLE);
                            order_details_btn1.setText("再次购买");
                            order_details_btn1.setBackground(getDrawable(R.drawable.my_order_red));
                            order_details_btn2.setText("删除订单");
                            order_details_btn2.setBackground(getDrawable(R.drawable.my_order_grey));
                        }
                    } else if(code == -1){
                        Toast.makeText(OrderDetailsActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(OrderDetailsActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        String  data = (String) map.get("message");
                        Toast.makeText(OrderDetailsActivity.this, data, Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(OrderDetailsActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }



    private void orderDetailsReturn() {
        order_details_return = findViewById(R.id.order_details_return);
        order_details_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }

    @Override
    protected void onStart() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            //不加这个clearFlags好像就没效果
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            //设置导航栏透明(就是虚拟键那一栏)

            //设置状态栏(或者叫通知栏)透明

            window.setStatusBarColor(Color.TRANSPARENT);
        }
        super.onStart();
    }
}
